package com.ypx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.ypx.model.Owner;
import com.ypx.model.Userinfo;
import com.ypx.service.*;
import com.ypx.util.JsonObject;
import com.ypx.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 * @Description 业主管理
 */
@Api(tags = {"业主管理"})
@RestController
@RequestMapping("/owner")
public class OwnerController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private IOwnerService ownerService;
    @Resource
    private IUserinfoService userinfoService;
    @Resource
    private IHouseService houseService;

    //todo 查询按照电话，身份证
    @RequestMapping("/queryOwnerAll")
    public JsonObject queryOwnerAll(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "15") Integer limit,
                                    Owner owner){
        PageInfo<Owner> pageInfo= ownerService.findOwnerAll(page,limit,owner);
        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());

    }

    @RequestMapping("/queryAll")
    @Transactional
    public List queryAll(){
        PageInfo<Owner> pageInfo= ownerService.findOwnerAll(1,100,null);
        return pageInfo.getList();
    }


    @ApiOperation(value = "新增")
    @RequestMapping("/add")
    public R add(@RequestBody Owner owner){
        owner.setPassword("000000");//默认密码000000
        int num= ownerService.add(owner);
        //同步添加到用户信息
        Userinfo user=new Userinfo();
        user.setPassword("000000");//默认密码
        user.setRemarks(owner.getRemarks());
        user.setType(0);//是否为业主
        user.setUsername(owner.getUsername());
        userinfoService.add(user);
        //拿到该业主居住的房屋的id
        Integer houseId = owner.getHouseId();
        houseService.updateStatusById(1,houseId);
        if(num>0){
            return R.ok();
        }else{
            return R.fail("添加失败");
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping("/deleteByIds")
    public R delete(String ids){
        List<String> list= Arrays.asList(ids.split(","));
        //遍历进行删除,并且把userinfo一同删除
        for(String id:list){
            //找到id对应的用户名
            Owner owner = ownerService.findById(Integer.parseInt(id));
            String username = owner.getUsername();

            userinfoService.deleteByUsername(username);
            houseService.updateStatusById(0,Integer.parseInt(id));
            //删除关联的数据
            ownerService.deleteOwner(Integer.parseInt(id));

        }
        return R.ok();
    }

    @ApiOperation(value = "更新")
    @RequestMapping("/update")
    public R update(@RequestBody Owner owner){

        int num= ownerService.updateData(owner);
        //如果业主更换了用户名，则userinfo一同修改
        Userinfo userinfo = new Userinfo();
        userinfo.setId(owner.getId());
        //则userinfo的username一同修改
        userinfo.setUsername(owner.getUsername());
        //则备注一同修改
        userinfo.setRemarks(owner.getRemarks());
        userinfoService.updateData(userinfo);

        //设置房间为入住状态
        Integer houseId = owner.getHouseId();
        houseService.updateStatusById(1,houseId);

        if(num>0){
            return R.ok();
        }else{
            return R.fail("修改失败");
        }
    }

    @GetMapping()
    public IPage<Owner> findListByPage(@RequestParam Integer page,
                                       @RequestParam Integer pageCount){
        return ownerService.findListByPage(page, pageCount);
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public Owner findById(@PathVariable Integer id){
        return ownerService.findById(id);
    }

}
